<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Models\Article\ArticleClassify;
use App\Models\Constant\Constant;
use App\Models\Setting\SysSetting;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

abstract class CommonController extends Controller {

    protected $getCol = ['*'];

    protected function handleChildren(&$records) {
        foreach ($records as $record) {
            if(count($record['children'])<=0){
                unset($record['children']);
            }else{
                $this->handleChildren($record['children']);
            }
        }
    }

    protected function getArticleClassifies($from){
        cache()->remember('classifies', 3600*6, function () use ($from) {
            $classifies =  (new ArticleClassify())->getLst(['pid'=>0])->with('children')->get(['id','name','pid','sort']);
            if($from==Constant::FROM_CODE){
                foreach ($classifies as $k=>&$item){
                    if(Str::contains($item->name,'PPT'))
                        unset($classifies[$k]);
                }
            }
            return $classifies->toArray();
        });
        $classifies = Cache::get('classifies');
        $this->handleChildren($classifies);
        return $classifies;
    }

    public function getMemberType(){
        cache()->remember('memberType', 3600*1, function () {
            return SysSetting::whereIn('key',['first','month','quarter','year'])->pluck('item_desc','key')->toArray();
        });
        return Cache::get('memberType');
    }
    public function getExMemberType(){
        cache()->remember('exmemberType', 3600*1, function () {
            return SysSetting::whereIn('key',['exfirst','exmonth','exquarter','exyear'])->pluck('item_desc','key')->toArray();
        });
        return Cache::get('exmemberType');
    }


}
